package com.web.service;


import com.web.VO.MyInfoVO;
import com.web.VO.MyPublishVO;
import com.web.VO.MyVideoVO;
import com.web.dao.UserDataMapper;
import com.web.entity.*;
import com.web.utils.GetLocation;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class UserDataService {
    @Resource
    private UserDataMapper userDataMapper;

    /**
     * 添加发布资料
     * @param userData
     */
    public void insertSelective(UserData userData) {
        userDataMapper.insertSelective(userData);
    }

    /**
     * 用户案例
     * @param userId
     * @return
     */
    public List<UserCaseList> getUserCaseList(String userId) {
        return userDataMapper.getUserCaseList(userId);
    }

    /**
     * 用户基础详情(包含企业资料)
     * @param userId
     * @return
     */
    public UserDetails getUserDetails(String userId) {
        UserDetails userDetails = userDataMapper.getUserDetails(userId);
        userDetails.setPersonalPerfection(userDataMapper.PersonalInformationPerfection(userId));
        userDetails.setEnterprisePerfection(userDataMapper.EnterprisePerfection(userId));
        return userDetails;
    }


    /**
     * 个人主页
     * @param userId
     * @return
     */
    public UserDetail getUserDetail(String userId) throws IOException {
        UserDetail userDetail = userDataMapper.getUserDetail(userId);
        String city = userDetail.getCity().substring(0,1);
        Pattern digit = Pattern.compile("\\d+");

        if (digit.matcher(city).matches()) {
            String [] position = userDetail.getCity().split(",");
            String longitude = position[0];//经度
            String latitude = position[1];//纬度
            userDetail.setLatitude(latitude);
            userDetail.setLongitude(longitude);
            userDetail.setCity((GetLocation.getCityName(GetLocation.getAdd(position[1],position[0]))));
            return userDetail;
        } else {
            String [] position = GetLocation.getCoordinate(userDetail.getCity());

            String longitude = position[0];//经度
            String latitude = position[1];//纬度
            userDetail.setLatitude(latitude);
            userDetail.setLongitude(longitude);
            return userDetail;
        }

    }

    /**
     * 我发布的技术资料
     * @param userId
     * @return
     */
    public List<MyPublishVO> listMyPublishInfo(String userId) {
        return userDataMapper.listMyPublishInfo(userId);
    }

    /**
     * 我发布的技术视频
     * @param userId
     * @return
     */
    public List<MyPublishVO> listMyPublishVideo(String userId) {
        return userDataMapper.listMyPublishVideo(userId);
    }



}
